/*=====================================================================

  File:      WireframeControl.cs
  Summary:   This control displays wireframes
  Date:	     May 27, 2006

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.Samples.SqlServer.DB3D
{
    // This is a simple WinForms user-control that renders wireframes by
    // simply delegating to the Wireframe class and dealing with details
    // such as picking the right size, deal with re-sizing, adjusting
    // the scale to make the drawing fit, etc.

    public partial class WireframeControl : UserControl
    {
		Wireframe _wireframe = new Wireframe();
		Pen _pen;
		bool _autoScale;

        public WireframeControl() {
            InitializeComponent();

			_pen = new Pen(this.ForeColor);
        }

        public override Color ForeColor {
			get {
				return base.ForeColor;
			}
			set {
				base.ForeColor = value;
				_pen = new Pen(this.ForeColor);
			}
		}

        public Wireframe Wireframe {
            get { return _wireframe; }
            set { _wireframe = value; }
        }

		public bool AutoScale {
			get { return _autoScale; }
			set {
				_autoScale = value;
				if(_autoScale) ScaleToFit(); // calls Refresh()
			}
		}

        public void ScaleToFit() {
            double scale = _wireframe.ComputeScale(this.Width, this.Height, (float).9);
            _wireframe.Scale = scale;
            Refresh();
        }

        public double RotateX {
            get { return _wireframe.RotateX; }
            set { _wireframe.RotateX = value; }
        }

        public double RotateY {
            get { return _wireframe.RotateY; }
            set { _wireframe.RotateY = value; }
        }

        public double RotateZ {
            get { return _wireframe.RotateZ; }
            set { _wireframe.RotateZ = value; }
        }

        public void Clear() {
			_wireframe.Clear();
			Refresh();
		}

        private void WireframeControl_SizeChanged(object sender, EventArgs e) {
			if(_autoScale)
				ScaleToFit(); // calls Refresh()
			else
				Refresh();
        }

        private void WireframeControl_Paint(object sender, PaintEventArgs e) {
			_wireframe.Render(e.Graphics, _pen, this.Width, this.Height);
        }
    }
}
